package com.codedog.enums;

import cn.hutool.core.util.StrUtil;

public enum ResponseCodeEnum {

    OK(200, "OK"),

    BAD_REQUEST_PARAM(400, "请求参数错误"), DATE_FORMAT_ERROR(401, "时间日期格式错误"),

    NOT_FOUND(404, "资源无法找到"),

    SYSTEM_ERROR(500, "系统错误"),

    COOKIE_INVALID(501, "Cookie失效"),

    FILE_DOWNLOAD_ERROR(502, "文件下载失败"), FILE_SYSTEM_ERROR(503, "另一个程序正在使用此文件，进程无法访问。"), SERVER_INSUFFICIENT_AUTHENTICATION_EXCEPTION(10010000, "Cookie失效"),


    ;


    private int code;
    private String msg;

    ResponseCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static String getMsgByCode(Integer code) {
        for (ResponseCodeEnum responseCodeEnum : values()) {
            if (responseCodeEnum.code == code) {
                return responseCodeEnum.getMsg();
            }
        }
        return ResponseCodeEnum.COOKIE_INVALID.getMsg();
    }

    public static ResponseCodeEnum getResponseCodeEnumByMsg(String msg) {
        if (StrUtil.isBlank(msg)) {
            return ResponseCodeEnum.COOKIE_INVALID;
        }
        for (ResponseCodeEnum responseCodeEnum : values()) {
            if (responseCodeEnum.getMsg().equals(msg) || msg.contains(responseCodeEnum.getMsg())) {
                return responseCodeEnum;
            }
        }
        return ResponseCodeEnum.COOKIE_INVALID;
    }

}